(function() {
    "use strict";

    /**
     * @ngdoc directive
     * @name webApp.directive:loginDialog
     * @description
     * # loginDialog
     */
    angular.module("webApp").directive("getuseropen", [
        "$rootScope",
        "helper",
        function($rootScope, helper) {
            return {
                restrict: "EA",
                template: '<span><a ng-click="init()" class="sui-btn btn-primary">+{{"添加员工"|translate}}</a></span>',
                scope: {
                    backcall: "&"
                },
                controller: [
                    "$scope",
                    "authService",
                    "sessionService",
                    "dataService",
                    "$compile",
                    "helper",
                    "$templateCache",
                    "base64",
                    "$timeout",
                    "ERP_CONFIG",
                    function(
                        $scope,
                        authService,
                        sessionService,
                        dataService,
                        $compile,
                        helper,
                        $templateCache,
                        base64,
                        $timeout,
                        ERP_CONFIG
                    ) {
                        $scope.init = function() {
                            $scope.initData().then($scope.showLogin);
                        };
                        $scope.initData = function() {
                            //随机码
                            if (ERP_CONFIG.debug) {
                                $scope.randkey = "1212fdsafrgsafsdaf";
                            } else {
                                $scope.randkey = helper.guid24();
                            }

                            var dd = ERP_CONFIG.eprUrl;
                            if (ERP_CONFIG.eprUrl.indexOf("https://") === 0) {
                                dd = ERP_CONFIG.eprUrl.replace("https://", "http://");
                            }
                            if (ERP_CONFIG.eprUrl.indexOf("//") === 0) {
                                dd = "http:" + ERP_CONFIG.eprUrl;
                            }
                            console.error("dd", dd);
                            if (ERP_CONFIG.debug) {
                                $scope.jastring =
                                    dd + "/web/dhfshop/?backlogin=" + $scope.randkey;
                            } else {
                                $scope.jastring = dd + "/web/?backlogin=" + $scope.randkey;
                            }
                            console.error("code", $scope.jastring);
                            return $scope.getImage($scope.jastring);
                        };

                        $scope.getImage = function(jastring) {
                            if (ERP_CONFIG.debug) {
                                return dataService
                                    .webQrcodeGet({
                                        code: jastring
                                    })
                                    .then(function(redata) {
                                        $scope.dataimage = "data:image/png;base64," + redata;
                                        return redata;
                                    });
                            } else {
                                return dataService
                                    .acodeunlimitGet(
                                        ERP_CONFIG.BackAppId,
                                        "AUTR" + $scope.randkey,
                                        true,
                                        "pages/admin/index/index"
                                    )
                                    .then(function(r) {
                                        $scope.dataimage = "data:image/png;base64," + r;
                                        return r;
                                    });
                            }
                        };

                        $scope.showLogin = function() {
                            //弹出
                            $timeout(function() {
                                $scope.checking = true;
                                $scope.showalert = $.alert({
                                    backdrop: "static",
                                    keyboard: true,
                                    title: '<span >{{"被添加的员工使用微信扫码"|translate}}</span>',
                                    body: '<div class="sui-form form-horizontal" style="text-align: center;"><img ng-src="{{dataimage}}" width="300" /> <br /> <button class="sui-btn btn-link" ng-click="initData()">刷新</button></div>',
                                    okBtn: '{{"ok" | translate}}',
                                    closeBtn: true,
                                    hasfoot: false,
                                    transition: true,
                                    width: "small",
                                    show: function(e) {
                                        getToken();
                                        $compile($(e.currentTarget).contents())($scope);
                                        return true;
                                    },
                                    cancelHidden: function() {
                                        //g关闭弹窗执行的动作
                                        $scope.checking = false;
                                    }
                                });
                            });
                        };

                        function getToken() {
                            dataService
                                .webTokenGet({
                                    identity_id: $scope.randkey
                                }, true)
                                .then(function(redata) {
                                    if (redata.token) {
                                        $scope.checking = false;
                                        console.log("查询的token数据========", redata);
                                        $scope.backcall({
                                            open_id: redata.open_id
                                        });
                                        $scope.showalert.modal('hide');
                                    } else {
                                        if ($scope.checking) {
                                            $timeout(getToken, 1000);
                                        }
                                    }
                                });
                        }
                    }
                ],

                link: function(scope, elem, attrs, ctr) {}
            };
        }
    ]);
})();